package io.renren.modules.diycake.service.impl;

import io.renren.modules.diycake.service.CakeService;
import io.renren.modules.diycake.vo.MaterialVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.diycake.dao.MaterialDao;
import io.renren.modules.diycake.entity.MaterialEntity;
import io.renren.modules.diycake.service.MaterialService;


@Service("materialService")
public class MaterialServiceImpl extends ServiceImpl<MaterialDao, MaterialEntity> implements MaterialService {
@Autowired
private CakeService cakeService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<MaterialEntity> wrapper = new QueryWrapper<>();
        String key = ""+params.get("key");
        wrapper.like("name",key);
        IPage<MaterialEntity> page = this.page(
                new Query<MaterialEntity>().getPage(params),
                wrapper
        );
        List<MaterialVo> collect = page.getRecords().stream().map(item -> {
            MaterialVo materialVo = new MaterialVo();
            BeanUtils.copyProperties(item, materialVo);
            materialVo.setCakeName(cakeService.getById(item.getCakeId()).getName());
            return materialVo;
        }).collect(Collectors.toList());

        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

}